package com.zqs.compareface.utils.impl.threadpool;

import com.zqs.compareface.utils.impl.ImageToBase64Util;
import com.zqs.compareface.utils.impl.threadpool.myLinkedBlockingQueue.ReWritedCapacityLinkedBlockingQueue;

import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

public class ThreadPoolAutoScalingTemplate {
    static ThreadPoolExecutor executor = TheCustomDynamicThreadPoolUtil.buildThreadPoolExecutor();
    static ReWritedCapacityLinkedBlockingQueue reWritedCapacityLinkedBlockingQueue = (ReWritedCapacityLinkedBlockingQueue) executor.getQueue();

    /**
     * 要执行的任务示例
     */
    void testTask(){
        int b =1+2;
    }
    /**
     * Submit方式提交线程池，自动动态伸缩
     */
    public void threadPool() {
        Future future = executor.submit(() -> {
            //线程要执行的任务
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "视频处理任务---");
        });
        try {
            future.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(executor.getActiveCount());
        //线程池调整
        if (executor.getActiveCount() / executor.getMaximumPoolSize() >= 0.7) {
            //最小核心线程数和最大线程数的值，设置为相同最佳
            executor.setCorePoolSize(Double.valueOf(Math.round(executor.getCorePoolSize() * 1.5)).intValue());
            executor.setMaximumPoolSize(Double.valueOf(Math.round(executor.getMaximumPoolSize() * 1.5)).intValue());
            executor.prestartAllCoreThreads();
            reWritedCapacityLinkedBlockingQueue.setCapacity(Double.valueOf(Math.round(reWritedCapacityLinkedBlockingQueue.getCapacity() * 1.2)).intValue());
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "线程池扩容之后");
            //有的任务不能合并线程，合并了就无法运行,如果使用的是executor.execute 而不是executor.submit 则不需要合并线程及其结果;submit()有返回值，而execute()没有
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue());
        if (executor.getActiveCount() / executor.getMaximumPoolSize() < 0.6) {
            //核心线程数是否大于2，如果大于2，判断活跃线程数扩大1.5倍后是否不为0（有可能在程序执行到此刻的时候，唯一一个执行任务的线程已经完成了任务，活跃线程就会为0）
            //如果核心线程数大于2为真，则核心线程数/2，或通过活跃线程数*1.5进行扩容（条件为，活跃线程数扩大1.5倍后是否为0
            //如果核心线程数大于2为假，那么直接设定线程数的最低值2  math.round 四舍五入取整
            executor.setCorePoolSize(executor.getCorePoolSize() > 2 ?
                    (Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue() == 0 ?
                            Double.valueOf(Math.round(executor.getCorePoolSize() / 2)).intValue() :
                            Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue()) : 2);
            executor.setMaximumPoolSize(executor.getMaximumPoolSize() > 2 ?
                    (Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue() == 0 ?
                            Double.valueOf(Math.round(executor.getMaximumPoolSize() / 2)).intValue() :
                            Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue()) : 2);
            executor.prestartAllCoreThreads();
            //如果队列大于5，则进行-5后的结果判断，如果-5后大于等于5，那么执行-5操作，如果大于等于5不成立，则直接修改队列数量为5
            reWritedCapacityLinkedBlockingQueue.setCapacity(reWritedCapacityLinkedBlockingQueue.getCapacity() > 5 ?
                    (Math.max(reWritedCapacityLinkedBlockingQueue.getCapacity() - 5, 5)) : 5);
//            (reWritedCapacityLinkedBlockingQueue.getCapacity() - 5 >= 5 ? reWritedCapacityLinkedBlockingQueue.getCapacity() - 5 : 5) : 5);
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "线程池收缩之后");
        }
    }
    /**
     * Execute方式提交任务，线程池自动动态伸缩
     */
    public void threadPoolExecuteTemplate() {
        executor.execute(() -> {
            // 执行任务
            testTask();
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "处理任务---");
        });
        System.out.println(executor.getActiveCount());
        //线程池调整
        if (executor.getActiveCount() / executor.getMaximumPoolSize() >= 0.7) {
            //最小核心线程数和最大线程数的值，设置为相同最佳
            executor.setCorePoolSize(Double.valueOf(Math.round(executor.getCorePoolSize() * 1.5)).intValue());
            executor.setMaximumPoolSize(Double.valueOf(Math.round(executor.getMaximumPoolSize() * 1.5)).intValue());
            executor.prestartAllCoreThreads();
            reWritedCapacityLinkedBlockingQueue.setCapacity(Double.valueOf(Math.round(reWritedCapacityLinkedBlockingQueue.getCapacity() * 1.2)).intValue());
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "线程池扩容之后");
        }
        System.out.println(Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue());
        if (executor.getActiveCount() / executor.getMaximumPoolSize() < 0.6) {
            //核心线程数是否大于2，如果大于2，判断活跃线程数扩大1.5倍后是否不为0（有可能在程序执行到此刻的时候，唯一一个执行任务的线程已经完成了任务，活跃线程就会为0）
            //如果核心线程数大于2为真，则核心线程数/2，或通过活跃线程数*1.5进行扩容（条件为，活跃线程数扩大1.5倍后是否为0
            //如果核心线程数大于2为假，那么直接设定线程数的最低值2  math.round 四舍五入取整
            executor.setCorePoolSize(executor.getCorePoolSize() > 2 ?
                    (Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue() == 0 ?
                            Double.valueOf(Math.round(executor.getCorePoolSize() / 2)).intValue() :
                            Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue()) : 2);
            executor.setMaximumPoolSize(executor.getMaximumPoolSize() > 2 ?
                    (Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue() == 0 ?
                            Double.valueOf(Math.round(executor.getMaximumPoolSize() / 2)).intValue() :
                            Double.valueOf(Math.round(executor.getActiveCount() * 1.5)).intValue()) : 2);
            executor.prestartAllCoreThreads();
            //如果队列大于5，则进行-5后的结果判断，如果-5后大于等于5，那么执行-5操作，如果大于等于5不成立，则直接修改队列数量为5
            reWritedCapacityLinkedBlockingQueue.setCapacity(reWritedCapacityLinkedBlockingQueue.getCapacity() > 5 ?
                    (Math.max(reWritedCapacityLinkedBlockingQueue.getCapacity() - 5, 5)) : 5);
            TheCustomDynamicThreadPoolUtil.threadPoolStatus(executor, "线程池收缩之后");
        }
    }
}
